package com.hhck.controller;

import com.hhck.constants.Constants;
import com.hhck.entity.CurrentUser;
import com.hhck.entity.Page;
import com.hhck.entity.Product;
import com.hhck.entity.Result;
import com.hhck.service.*;
import com.hhck.utils.TokenUtils;
import com.hhck.utils.UploadFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private StoreService storeService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private ProductTypeService productTypeService;

    @Autowired
    private SupplyService supplyService;

    @Autowired
    private PlaceService placeService;

    @Autowired
    private UnitService unitService;

    @Autowired
    private TokenUtils tokenUtils;

    // 图片上传本地路径
    @Value("${file.upload-path}")
    private String uploadPath;

    // 应用名
    @Value("${spring.application.name}")
    private String appName;

    /**
     * 分页查询商品信息（条件查询）
     */
    @GetMapping("/product-page-list")
    public Result productPageList(Page page, Product product) {
        return Result.ok(productService.queryProductByPage(page, product));
    }

    /**
     * 查询所有仓库
     */
    @GetMapping("/store-list")
    public Result storeList() {
        return Result.ok(storeService.queryAllStore());
    }

    /**
     * 查询所有品牌
     */
    @GetMapping("/brand-list")
    public Result brandList() {
        return Result.ok(brandService.queryAllBrand());
    }

    /**
     * 查询商品类型树
     */
    @GetMapping("/category-tree")
    public Result categoryTree() {
        return Result.ok(productTypeService.queryAllProductType());
    }

    /**
     * 查询所有供应商
     */
    @GetMapping("/supply-list")
    public Result supplyList() {
        return Result.ok(supplyService.queryAllSupply());
    }

    /**
     * 查询所有产地
     */
    @GetMapping("/place-list")
    public Result placeList() {
        return Result.ok(placeService.queryAllPlace());
    }

    /**
     * 查询所有单位
     */
    @GetMapping("/unit-list")
    public Result unitList() {
        return Result.ok(unitService.queryAllUnit());
    }

    /**
     * 上传图片
     * @param file MultipartFile前端传递的文件
     */
    // 允许跨域请求
    @CrossOrigin
    @PostMapping("/img-upload")
    public Result imgUpload(MultipartFile file) {

        // 调用自定义工具类
        boolean success = UploadFileUtil.uploadFile(file, file.getOriginalFilename());
        if (success) {
            return Result.ok("文件上传成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "文件上传失败！");
    }

    /**
     * 添加商品
     */
    @PostMapping("/product-add")
    public Result productAdd(@RequestBody Product product, @RequestHeader(Constants.HEADER_TOKEN_NAME) String token) {

        // 获取当前登录用户（创建者）
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        // 设置创建者
        product.setCreateBy(currentUser.getUserId());

        return productService.saveProduct(product);
    }

    /**
     * 修改商品上下架状态
     */
    @PutMapping("/state-change")
    public Result productStateChange(@RequestBody Product product) {
        return productService.modifyProductUpDownStateByProductId(product);
    }

    /**
     * 删除单个商品
     */
    @DeleteMapping("/product-delete/{productId}")
    public Result productDelete(@PathVariable Integer productId) {
        return productService.removeProductByProductId(productId);
    }

    /**
     * 批量删除商品
     */
    @DeleteMapping("/product-list-delete")
    public Result productListDelete(@RequestBody List<Integer> productIdList) {
        return productService.batchRemoveProductByProductIdList(productIdList);
    }

    /**
     * 修改商品
     */
    @PutMapping("/product-update")
    public Result productUpdate(@RequestBody Product product, @RequestHeader(Constants.HEADER_TOKEN_NAME) String token) {

        // 获取当前登录用户（修改者）
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        // 设置修改者
        product.setUpdateBy(currentUser.getUserId());

        return productService.modifyProductByProductId(product);
    }

}

